package com.ruoyi.back.domain.v3

import cn.hutool.core.bean.BeanUtil
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.back.mapper.VideoTemplateFaceMapper
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import org.springframework.stereotype.Service
import javax.persistence.*

@TableName("fly_video_template_face")
@ApiModel("视频人脸模板")
@Table(name = "fly_video_template_face")
@Entity
data class VideoTemplateFace(

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @TableId(type = IdType.AUTO)
    var id: Long? = null,

    @ApiModelProperty("模板id")
    var templateId: String? = null,

    @ApiModelProperty("人脸对应的子模板ID")
    var templateFaceId: String? = null,

    @ApiModelProperty("人脸对应的子模版ID对应的人脸图")
    var templateFaceUrl: String? = null,
)

@ApiModel("视频子人脸")
data class VideoSubFace(
    @ApiModelProperty("人脸对应的子模板ID")
    var templateFaceId: String? = null,

    @ApiModelProperty("人脸对应的子模版ID对应的人脸图")
    var templateFaceUrl: String? = null,
)

@Service
class VideoTemplateFaceService : ServiceImpl<VideoTemplateFaceMapper, VideoTemplateFace>() {

    fun getVideoTemplateSubFace(templateId: String): List<VideoSubFace>? =
        list(
            KtQueryWrapper(VideoTemplateFace::class.java)
            .eq(VideoTemplateFace::templateId, templateId))
            .takeIf { it.isNotEmpty() }
            ?.run {

                distinctBy { it.templateFaceId }.map {
                    VideoSubFace().apply {
                        BeanUtil.copyProperties(it, this)
                    }
                }
            }
}